#include <stdio.h>
#include <stdlib.h>
#include "time.h"
#include "mystring.h"
#include "DynamicArray.h"
#define Map_size 10
#define false 0
#define true 1
typedef struct Player
{
    int HP;
    int attack;
    int monery;
    int x;
    int y;
    
}Player;

typedef struct Shore
{
   
    int name1;
    int name2;
    int value1;
    int value2;
    mystring namea;
    mystring nameb;
}Shore;

typedef struct Monster
{
    mystring name;
    int HP;
    int attack;
    int x;
    int y;
}Monster;

typedef struct Treasure
{
    mystring name;
    int value;
    int sign;
    int x;
    int y;
}Treasure;

void InitPlayer(Player *player)
{
    player->monery=0;
    player->HP=100;
    player->attack=10;
    player->x=0;
    player->y=0;
    
}

int InitShore(Shore *shore)
{

    shore->name1=10;
    shore->value1=400;
    shore->name2=50;
    shore->value2=200;
    shore->namea.string="宝剑";
    shore->nameb.string="药品";
}

int GetRandNumber(int max)
{
    return rand() % max;
}

Monster* CreateMonster(const char*name,int hp,int attack)
{
    Monster* monster=(Monster*)malloc(sizeof(Monster));
    if(monster==NULL)
    {
        printf("monster mallooc error!\n");
        return NULL;
    }
    Init_string(&monster->name,name);
    monster->HP=hp;
    monster->attack=attack;
    monster->x=GetRandNumber(Map_size);
    monster->y=GetRandNumber(Map_size);
    return monster;
}

Treasure*CreateTreasure(const char *name,int value,int sign)
{
    Treasure *treasure=(Treasure*)malloc(sizeof(Treasure));
    if(treasure==NULL)
    {
        printf("treasure malloc error\n");
        return NULL;
    }
    Init_string(&treasure->name,name);
    treasure->value=value;
    treasure->sign=sign;
    treasure->x=GetRandNumber(Map_size);
    treasure->y=GetRandNumber(Map_size);
    return treasure;
}

DynamicArray *CreateDynamicAarray(DynamicArray *array,int  number)
{
    array->dp=malloc(sizeof(void*)*number);
    if(array->dp==NULL)
    {
        printf("DynamicArray malloc error\n");
        return NULL;
    }
    array->size=number;
    array->len=0;
}

int InitMonsters(DynamicArray *monster)
{
    if(Initialize(monster) == false)
    {
        printf("InitMonsters Error!\n");
        return false;    
    }                                
    InsertArray(monster,CreateMonster("巨龙",100,20));    
    InsertArray(monster,CreateMonster("半人马",50,10));    
    InsertArray(monster,CreateMonster("半人马",50,10));   
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("巨龙",100,20));    
    InsertArray(monster,CreateMonster("半人马",50,10));    
    InsertArray(monster,CreateMonster("半人马",50,10));   
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2));  
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2)); 
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("巨龙",100,20));    
    InsertArray(monster,CreateMonster("半人马",50,10));    
    InsertArray(monster,CreateMonster("半人马",50,10));   
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("巨龙",100,20));    
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("半人马",50,10));    
    InsertArray(monster,CreateMonster("半人马",50,10));   
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2));  
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2)); 
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("巨龙",100,20));    
    InsertArray(monster,CreateMonster("半人马",50,10));    
    InsertArray(monster,CreateMonster("半人马",50,10));   
    InsertArray(monster,CreateMonster("半人马",50,10));  
    InsertArray(monster,CreateMonster("巨龙",100,20));    
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    InsertArray(monster,CreateMonster("哥布林",10,2));   
    return true;
}

int InitTreasure(DynamicArray *treasure)
{
    if(Initialize(treasure)==false)
    {
        return false;
    }
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    InsertArray(treasure,CreateTreasure("金钱",100,1));
    InsertArray(treasure,CreateTreasure("药品",20,0));
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    InsertArray(treasure,CreateTreasure("金钱",100,1));
    InsertArray(treasure,CreateTreasure("药品",20,0));
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    InsertArray(treasure,CreateTreasure("金钱",100,1));
    InsertArray(treasure,CreateTreasure("药品",30,0));
    InsertArray(treasure,CreateTreasure("药品",20,0));
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    InsertArray(treasure,CreateTreasure("金钱",100,1));
    InsertArray(treasure,CreateTreasure("药品",20,0));
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    InsertArray(treasure,CreateTreasure("金钱",100,1));
    InsertArray(treasure,CreateTreasure("药品",30,0));
    InsertArray(treasure,CreateTreasure("药品",20,0));
    InsertArray(treasure,CreateTreasure("宝箱",300,1));
    return true;
}

void MakeMove(Player *player,int dx,int dy)
{
    player->x+=dx;
    player->y+=dy;
    if(player->x<0)
        player->x=0;
    if(player->x>=Map_size)
        player->x=Map_size-1;
    if(player->y<0)
        player->y=0;
    if(player->y>=Map_size)
        player->y=Map_size-1;
}

int Battle(Player *player,Monster *monster)
{
    int i=monster->HP;
    printf("您遭遇到了%s 怪兽血量%d 攻击力%d\n",monster->name.string,monster->HP,monster->attack);
    
    while(1)
    {
        printf("请选择您的行动 1、攻击 2、防御 3、逃跑\n");
        printf("请选择:");
        int  choice=0;
        scanf(" %d",&choice);
        switch(choice)
        {
            case  1:
                printf("你对怪兽造成了%d伤害\n",player->attack);
                if(GetRandNumber(2)==1)
                {
                    printf("怪兽对你进行了还击，造成了%d伤害\n",monster->attack);
                    player->HP-=monster->attack;
                }
                monster->HP-=player->attack;
                break;
            case  2:
                printf("怪兽对你进行了攻击\n");
                if(GetRandNumber(3)!=1)
                {
                    printf("你成功防御住了怪兽的攻击\n");
                }
                else
                {
                    printf("防御失败，怪兽对你造成了%d伤害\n",monster->attack);
                    player->HP-=monster->attack;
                }
                break;
            case 3:
                if(GetRandNumber(2)==1)
                {
                    printf("逃跑成功\n");
                    monster->HP=0;
                    return true;
                }
                else
                {
                    printf("逃跑失败\n");
                    printf("怪兽对你进行了还击，造成了%d伤害\n",monster->attack);
                    player->HP-=monster->attack;
                }
                break;
            default:
                break;
        }
        if(player->HP<=0)
        {
            printf("怪兽打败了你\n");
            return false;
        }
        if(monster->HP<=0)
        {
            player->monery+=i*2;
            printf("你战胜了怪兽\n");
            printf("你获得了%d金钱\n",i*2);
            
            return true;
        }
        printf("当前你的血量%d\n",player->HP);
        printf("当前怪物血量%d\n",monster->HP);
    }
}

void PersonalAttibutes(Player *player)
{
    system("clear");
    while(1)
    {
        
        char a='0';
        scanf("%c",&a);
        printf("生命值：%d\n",player->HP);
        printf("攻击力：%d\n",player->attack);
        printf("金钱：%d\n",player->monery);
        printf("                                         按't'退出\n");
        
        if(a=='t')
        {
            system("clear");
            return ;
        }
    }
}

void StoreCenter(Shore *shore,Player *player)
{
    system("clear");
    while(1)
    {
        
        char a='0';
        scanf("%c",&a);
        printf("%s :   价格：%d    攻击力加%d\n",shore->namea.string,shore->value1,shore->name1);
        printf("                            购买请按'1'\n");
        printf("%s :   价格：%d    生命力加%d\n",shore->nameb.string,shore->value2,shore->name2);
        printf("                            购买请按'2'\n");
        printf("                                          按't'退出\n");
        if(a=='t')
        {
            system("clear");
            return ;
        }
            
        if(a=='1'&&player->monery>=shore->value1)
        {
            player->monery-=shore->value1;
            player->attack+=shore->name1;
            printf("购买成功！\n");
        }

        if(a=='2'&&player->monery>=shore->value2)
        {
            player->monery-=shore->value2;
            player->HP+=shore->name2;
            printf("购买成功！\n");
        }
    }
}



int main()
{
    srand(time(NULL));
    Player player; 
    InitPlayer(&player);
    Shore shore;
    InitShore(&shore);
    DynamicArray monster={NULL,10,0};
    InitMonsters(&monster);
    DynamicArray treasure={NULL,10,0};
    InitTreasure(&treasure);
    printf("欢迎来到魔塔游戏\n");
    while(1)
    {
        
        printf("您当前所在的位置是:<%d %d>\n",player.x,player.y);
        for(int i=0;i<monster.len;i++)
        {
            Monster *monsters=(Monster*)monster.dp[i];
            if(monsters->HP>0&&monsters->x==player.x&&monsters->x==player.y)
            {
                if(Battle(&player,monsters)==false)
                {
                    printf("游戏失败\n");
                    return false;
                }
            }
        }
        for(int i=0;i<treasure.len;i++)
        {
            Treasure *treasures=(Treasure *)treasure.dp[i];
            if(treasures->value!=0&&treasures->x==player.x&&treasures->y==player.y)
            {
                printf("你找到了%s\n",treasures->name.string);
                if(treasures->sign==1)
                    printf("获得了%d金币\n",treasures->value);
                if(treasures->sign==0)
                    printf("增加了%d生命值\n",treasures->value);
                if(treasures->sign==0)
                {
                    player.HP+=treasures->value;
                }
                if(treasures->sign==1)
                {
                    player.monery+=treasures->value;
                }
                treasures->value=0;
            }
        }
        printf("请按'w' 's' 'a' 'd'进行移动\n");
        printf("按'q'查看个人属性\n");
        printf("按'p'进入商店\n");
        char move;
        scanf(" %c",&move);
        switch (move)
        {
            case 'w':
                MakeMove(&player,0,1);
                break;
            case 's':
                MakeMove(&player,0,-1);
                break;
            case 'a':
                MakeMove(&player,-1,0);
                break;
            case 'd':
                MakeMove(&player,1,0);
                break;
            case 'q':
                PersonalAttibutes(&player);
                break;
            case 'p':
                StoreCenter(&shore,&player);
                break;
            default:
                break;
        }
    }
    for(int i=0;i<monster.len;i++)
    {
        free((Monster*)monster.dp[i]);
    }
    free(monster.dp);
    for(int i=0;i<treasure.len;i++)
    {
        free((Treasure*)treasure.dp[i]);
    }
    free(treasure.dp);
    return 0;
}